package com.hua.yuan.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.hua.yuan.R;
import com.hua.yuan.ui.VideoDisActivity;
import com.hua.yuan.ui.module.MovieRotBean;
import com.hua.yuan.util.CheckLoginUtil;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.List;

/**
 * Created by huangyang on 2018/5/8.
 * 自定义的bannerView
 */

public class MyBannerView extends RelativeLayout {

    private MZBannerView bannerView;

    private int type_ = 0;

    private void init(Context context) {

        LayoutInflater.from(context).inflate(R.layout.video_banner_item_view, this);
        bannerView = (MZBannerView) findViewById(R.id.banner);

    }


    public MyBannerView(Context context) {
        super(context);
        init(context);
    }

    public MyBannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public MyBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }


    @SuppressLint("NewApi")
    public MyBannerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }



    public void setData(List<MovieRotBean> list){

        try {
            if (bannerView != null) {
                bannerView.setPages(list, new MZHolderCreator<BannerViewHolder>() {
                    @Override
                    public BannerViewHolder createViewHolder() {
                        return new BannerViewHolder();
                    }
                });

//                bannerView.setDelayedTime(4000);
                bannerView.start();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static class BannerViewHolder implements MZViewHolder<MovieRotBean> {

        private ImageView mImageView;

        @Override
        public View createView(Context context) {

            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;

        }

        @Override
        public void onBind(final Context context, int i, final MovieRotBean bean ) {

            Glide.with(context)
                    .load(bean.getImg_url())
                    .into(mImageView);


            mImageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (CheckLoginUtil.check()){
                        context.startActivity(new Intent(context, VideoDisActivity.class).putExtra("id",bean.getId()));
                    }else {
                        CheckLoginUtil.goToLogin((Activity) context);
                    }


                }
            });


        }
    }


}
